
/**
 * required selectbutton.js
 */
if (!QSAPI){
	var QSAPI={};
}
QSAPI.QuoteSettingPanel=function(container,callback,config){
    //this.quoteObj=quoteObj;
	var t=QS_Lang.quote;
    this.container=container;
    this.callback=callback||{};
	this.config={
		'position':'bottom'
	};
	if(config){
		$.extend(this.config,config);
	}
    this.el=null;
    this.sltAllBtnEl=null;
    this.dataPointChk=null;
    this.scrollbar=null;
    this.width=null;
    this.height=null;
    //this.dataPointList=null;
    this.html='<div class="rtq-quotes-view"><h3>'+t.curview+'</h3><div class="rtq-quotesetting-hd"></div><div class="bd"><div class="datarow"></div></div><div class="rtq-quotesetting-ft"></div></div>';
//    this.init();
};

QSAPI.QuoteSettingPanel.prototype={
    init:function(data,sequence){
        this.el=$(this.html);
        if(this.container){
           this.el.appendTo(this.container);
        };
		this._buildSelectAllEl();
        this.sltAllBtnEl = this.el.find('.sltall');
        this._createDataPointChk(data); 
        this._initEvent(); 
        this._initScrollBar();
    },
	_buildSelectAllEl:function(){
		var t=QS_Lang.quote;
		var html='<div class="rtq-quote-operation"><a href="javascript:void(0)" class="rtq-checkbox sltall">'+t.selectall+'</a></div>';
		if(this.config.position=='top'){
			this.el.find('.rtq-quotesetting-hd')[0].innerHTML=html;
		}else{
			this.el.find('.rtq-quotesetting-ft')[0].innerHTML=html;
		}
	},
    rebindData:function(data,sequence,needResize){
    	if (!this.dataPointChk){
    		return 
    	}
//        this.dataPointChk.bindData(data);
        if(data.length == sequence.length){
            this._selectAll();
            this.selectAllChkBtn(true);
        }else{
            this._inverseAll();
            this.selectAllChkBtn(false);
            for(var i=0,l=data.length;i<l;i++){
               this.dataPointChk.btns[data[i].val].select();
            }
        }
        this.resize(null,null,needResize);
    },
    getDom:function(){
       return this.el[0];
    },
    _initScrollBar:function(){
        this.scrollbar=new Util.ScrollPanel(this.el.find('.datarow'), {}); 
    },
    resize:function(w,h,flag){
        var flag=flag===true?true:false;
        w=w>0?w:this.width;
        h=h>0?h:this.height;
        if(w!=this.width){
            this.width=w;
            flag=true;
        }
        if(h!=this.height){
            this.height=h;
            flag=true;
        }
        if (flag) {
            this.scrollbar.reSize(this.width, this.height);
        }
    },
    _initEvent:function(){
        var self=this;
       this.sltAllBtnEl.bind('click.QuoteSetting',function(e){
            var target=$(e.target);
            var isSelected=false;
            if(target.hasClass('rtq-checkbox-on')){
                target.removeClass('rtq-checkbox-on');
                self._inverseAll();
            }else{
                target.addClass('rtq-checkbox-on');
                self._selectAll();
                isSelected=true;        
            }
            if(typeof self.callback.onClickAll=='function'){
                self.callback.onClickAll(isSelected);
            }
       });
    },
    _inverseAll:function(){
        for(var v in this.dataPointChk.btns){
            this.dataPointChk.btns[v].inverse();
        }
    },
    _selectAll:function(){
        for(var v in this.dataPointChk.btns){
            this.dataPointChk.btns[v].select();
        }
    },
    _createDataPointChk:function(data){
        var self=this;
        this.dataPointChk=new Util.ButtonGroup(this.el.find('.datarow'),data, {
            onClick: function(v, item){                
                if(item.isSelected){
                    if(self._isSelectAll()){
                        self.sltAllBtnEl.addClass('rtq-checkbox-on');
                    }  
                }else{
                    self.sltAllBtnEl.removeClass('rtq-checkbox-on');
                }
                if(typeof self.callback.onClick=='function'){
                    self.callback.onClick(v,item.isSelected);
                }
            }
        },{type:'checkbox',vertical:true});
    },
    _isSelectAll:function(){
        var ret=true;
        for(var v in this.dataPointChk.btns){
            if(!this.dataPointChk.btns[v].el.hasClass('rtq-checkbox-on')){
                ret=false;
                break;
            }
        }
        return ret;
    },
    selectDataPointChk:function(vals,isSelect){
        if(this.dataPointChk==null){
            return;
        }
        isSelect=isSelect===false?false:true;
        for(var i=0,l=vals.length;i<l;i++){
            if (isSelect) {
                this.dataPointChk.btns[vals[i]].select();
            }else{
                this.dataPointChk.btns[vals[i]].inverse();
            }
        }
    },
    selectAllChkBtn:function(isSelect){
        if(isSelect){
            this.sltAllBtnEl.addClass('rtq-checkbox-on');
        }else{
            this.sltAllBtnEl.removeClass('rtq-checkbox-on');
        }
    },
    clear:function(){
        this.sltAllBtnEl.unbind('.QuoteSetting');
        for(var p in this){
            if(!p || p == 'clear'){continue;}
            this[p] = null;
            delete this[p];
       }
    }
};
